package com.srv.persistence.impl;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.srv.Config;
import com.srv.ContentType;
import com.srv.model.Resource;
import com.srv.persistence.IResourcePersistence;
import com.srv.persistence.PersistenceException;
import com.srv.persistence.impl.cloud.AliyunOSS;

public class DefaultResourcePersistence implements IResourcePersistence {
	private static Log log = LogFactory.getLog(DefaultResourcePersistence.class);
	private static final String FXG="/";

	/**
	 * 保存资源对象.
	 * @param resource 
	 * @see com.srv.model.Resource
	 */
	public ArrayList<String> save(
			Resource resource	   		   
			   ) throws com.srv.persistence.PersistenceException{
		//save to disk
		  ArrayList<String> al = new ArrayList<String>();
		  if(resource.id==null||resource.name==null||resource.content==null||resource.content.length==0){
			  return al;
		  }		  
		  /****************************** SAVE TO LOCAL FILE *************************/
		  try{
			  String saveFile=Config.getResource_disk_root().concat(FXG).concat(resource.id);
			  File tf = new File(saveFile);
			  if(!tf.exists()){
				  tf.mkdir();
			  }		 		  
			  saveFile=saveFile.concat(FXG).concat(resource.name);		  
			  log.debug("the resource save to file://"+saveFile);
			  try{
				  File file = new File(saveFile);
				  FileOutputStream fos = new FileOutputStream(file);
				  BufferedOutputStream bos = new BufferedOutputStream(fos);
				  bos.write(resource.content);
			  }catch(Exception e){
				  log.fatal(e.getMessage());
			  }
			  al.add("file://".concat(saveFile));
		  }catch(IOException ioe){
				log.error("error:"+ioe.getMessage());
				throw new PersistenceException();
			}
		  
		  /****************************** SAVE TO Aliyun OSS *************************/
		  try{
			  al.add(AliyunOSS.save(resource));
		  }catch(com.srv.persistence.PersistenceException pe){
			  log.error("error: "+pe.getMessage());
		  }
		  return al;
	}
	/**
	 * 获取资源对象.
	 * @return Resource 
	 * @see com.srv.model.Resource
	 */
	public Resource getResource(
			String resourceID,
			String resourceName
	) throws com.srv.persistence.PersistenceException{
		try{
			Resource r = null;		
			String saveFile= null;
			saveFile=Config.getResource_disk_root().concat(FXG).concat(resourceID).concat(FXG).concat(resourceName);		
			File tf = new File(saveFile);
			//如果本地有，就从本地取，否则从云上取
			if(tf.exists()){	
			log.debug(resourceID+"/"+resourceName+" is from local disk");
				FileInputStream fis=new FileInputStream (tf);
				if(fis!=null){
					r = new Resource();
					int len=fis.available();
					r.content=new byte[len];
					fis.read(r.content);
				    fis.close();
				    r.contentType=ContentType.getIntByFileName(saveFile);
				    r.id=resourceID;
				    r.name=resourceName;
					return r;
				}
			}		
		}catch(IOException ioe){
			log.error("error:"+ioe.getMessage());
			throw new PersistenceException();
		}
		//从云
		try{
			log.debug(resourceID+"/"+resourceName+" is from cloud");
			return AliyunOSS.getObject(resourceID, resourceName);
		}catch(Exception e){
			log.error("error: "+e.getMessage());
			throw new PersistenceException();
		}
	}
	/**
	 * 移除资源
	 * @param urlList
	 * @throws IOException
	 * @throws com.srv.persistence.PersistenceException
	 */
    public void remove(List<String> urlList)throws com.srv.persistence.PersistenceException{
    	
    }

}
